package com.yd.mvc.pc.roleController;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.pc.roleService.RoleService;
import com.yd.app.util.BusinessException;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.*;
import com.yd.domain.searchParam.RoleParam;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-07 15:50
 * @desc 角色
 **/
@RestController
@RequestMapping(value = "/api/role")
public class RoleController extends BaseController{
    @Autowired
    private RoleService roleService;
    @Autowired
    private LoggerService loggerService;
    /**
     * @Author: zy
     * @Desc:角色的录入添加
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addRole(@RequestBody Role r,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.ROLE_MANAGER);//验证权限
        }
        if(StringUtils.isEmpty(r.getCode())){
            throw new BusinessException(500,"编码不能为空");
        }else{
            //是否已存在
            Role role= roleService.getRoleByID(r.getCode());
            if(role!=null){
                throw new BusinessException(500,"编码已存在");
            }
        }
        String userName= user.getUserName();
        String suff="角色代码:"+r.getCode()+";角色名称:"+r.getTitle()+";描述:"+r.getDes();
        try {
            roleService.addRole(r);
            loggerService.addLogger(new Logger("新增角色记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("新增角色记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/addUserRole",method = RequestMethod.POST)
    public void addUserRole(@RequestBody UserRole ur, HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.ROLE_MANAGER);//验证权限
        }
        roleService.addUserRole(ur);
    }
    @RequestMapping(value = "/deleteUserRole",method = RequestMethod.POST)
    public void deleteUserRole(@RequestBody UserRole ur, HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.ROLE_MANAGER);//验证权限
        }
        roleService.deleteUserRole(ur);
    }
    /**
     * @Author: zy
     * @Desc:删除角色
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/delete/{code}",method = RequestMethod.DELETE)
    public void deleteRoleByCode(@PathVariable("code") String code,HttpServletRequest request) throws UnsupportedEncodingException, BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.ROLE_MANAGER);//验证权限
        }
            String userName=user.getUserName();
            String suff="ID:"+code;
        try {
            roleService.deleteRoleByCode(code);
                loggerService.addLogger(new Logger("删除角色记录",userName,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
        } catch (Exception e) {
                loggerService.addLogger(new Logger("删除角色记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc:修改角色
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public void updateRole(@RequestBody  Role role,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.ROLE_MANAGER);//验证权限
        }
        String userName=user.getUserName();
        String suff="角色代码:"+role.getCode()+";角色名称:"+role.getTitle()+";描述:"+role.getDes();
        try {
            roleService.updateRole(role);
            loggerService.addLogger(new Logger("修改角色信息",userName,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("修改角色信息",userName,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc:根据code查询角色
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/find/{code}",method = RequestMethod.GET)
    public Role findRoleByCode(@PathVariable("code") String code){
        Role role =new Role();
        role= roleService.getRoleByID(code);
        return role;
    }
    /**
     * @Author: zy
     * @Desc:分页查询数据
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getOrgList(@RequestBody RoleParam roleParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=roleService.getRoleList(roleParam,pageIndex,pageSize);
        return pm;
    }
    /**
     * @Author: zy
     * @Desc:分页查询数据
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/alllist", method = RequestMethod.GET)
    public List<Role> getAllRoleList(){
        List<Role> list=roleService.getAllRoleList();
        return list;
    }
    /**
     * @Author: zy
     * @Desc:根据角色获取角色的权限
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/list/role/{roleId}/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageModel getPermListByRole(@PathVariable("roleId")String roleId,@PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=roleService.getPermListByRole(roleId,pageIndex, pageSize);
        return pm;
    }
    /**
     * @Author: zy
     * @Desc:根据角色获取角色尚未拥有权限
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/list/role/not/{roleId}/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageModel getNotHavePermListByRole(@PathVariable("roleId")String roleId,@PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=roleService.getNotHavePermListByRole(roleId,pageIndex, pageSize);
        return pm;
    }

    /**
     * @Author: zy
     * @Desc:角色添加权限
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/add/perm_to_role", method = RequestMethod.POST)
    public void addPermToRole(@RequestBody List<RolePerm> list,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.ROLE_MANAGER);//验证权限
        }
        String userName=user.getUserName();
        String suff="添加人:"+user;
        try {
            roleService.addPermToRole(list);
            loggerService.addLogger(new Logger("角色分配权限记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("角色分配权限记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 给角色删除权限
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/delete/perm_from_role", method = RequestMethod.POST)
    public void deletePermFromRole(@RequestBody RolePerm pr,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.ROLE_MANAGER);//验证权限
        }
        String userName=user.getUserName();
        String suff="角色Id:"+pr.getRoleId()+";权限code:"+pr.getPermCode();
        try {
            roleService.deletePermFromRole(pr);
            loggerService.addLogger(new Logger("删除角色权限记录",userName,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("删除角色权限记录",userName,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.ROLEMANAGE,"",suff),request);
            e.printStackTrace();
        }
    }
}
